class Solution:
    def longestBeautifulSubstring(self, word: str) -> int:
        vowels = ['a', 'e', 'i', 'o', 'u']
        cur = 0
        left = 0
        right = 0
        res = 0
        while right < len(word):
            while left < len(word) and word[left] != 'a':
                left += 1
                right = left
            if right >= len(word):
                break
            pre = vowels[min(cur, len(vowels)-1)]
            nxt = vowels[min(cur+1, len(vowels)-1)]
            if word[right] == pre or word[right] == nxt:
                if word[right] == 'u':
                    res = max(res, right-left+1)
                if word[right] == nxt:
                    cur += 1
            else:
                left = right
                cur = 0
            right += 1

        return res
